// pages/fr/fr.js
Page({
    data: {
      list:[],
      num:"",
      src: '',
      base64: "",
      baidutoken: "",
      msg: null
    },
    takePhoto:function(e) {
      const ctx = wx.createCameraContext()
      ctx.takePhoto({
        quality: 'high',
        success: res => {
          console.log(res)
          this.setData({
            src: res.tempImagePath // 这是自己拍的照片的路径
          })
          wx.getFileSystemManager().readFile({
            filePath: this.data.src, 
            encoding: 'base64', 
            success: res => {         
              this.setData({
                base64: res.data
              })
              this.checkPhoto();
            }
          })
        }
      })
      wx.showToast({
        title: '正在配对中~',
        icon: 'loading',
        duration: 500
      })
    },
    error(e) {
      console.log(e.detail)
    },
    checkPhoto() {
      var that = this;
      //acess_token获取
      wx.request({
        url: 'https://aip.baidubce.com/oauth/2.0/token', 
        data: {
          grant_type: 'client_credentials',
          client_id: 'W3GwBYFDXNj1M0AY57GjGSng', 
          client_secret: 'kRYVfePdurCGBu9Kb5hxxyGNGBuyDUr5' 
        },
        header: {
          'Content-Type': 'application/json' 
        },
        success(res) {
          that.setData({
            baidutoken: res.data.access_token 
          })
          that.validPhoto();
        }
      })
    },
    validPhoto() {
      //上传人脸进行 比对
      wx.request({
        url: 'https://aip.baidubce.com/rest/2.0/face/v3/search?access_token='+ this.data.baidutoken,
        method: 'POST',
        data: {
          image: this.data.base64,
          image_type: 'BASE64',
          group_id_list: '26575168', 
        },
        header: {
          'Content-Type': 'application/json' 
        },
        success:res=> {
          console.log(res)
          this.setData({
            msg: res.data.error_msg
          })
          if (this.data.msg == "pic not has face") {
            wx.showToast({
              title: '未捕获到人脸',
              icon: 'error',
            })
          }
          if (this.data.msg == 'SUCCESS') {
            if(res.data.result.user_list[0].score>50){
              wx.showToast({
                title: '人脸匹配成功',
                icon: 'success',
                duration:3000,
              })
              var num = parseInt(res.data.result.user_list[0].score)
              this.setData({
                num:num
              })
            }else{
              wx.showToast({
                title: '人脸匹配失败',
                icon: 'error',
                duration:3000,
              })
              var num = parseInt(res.data.result.user_list[0].score)
              this.setData({
                num:num
              })
            }
          }
        }
      });
    }
  })
  